﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReportController.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Defines the ReportController type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.MVC.Controllers
{
    using System;
    using System.Linq;
    using System.Web.Mvc;
    using Helpers;

    using Spenta.StreetCare.MVC.Models;

    /// <summary>
    /// Defines the ReportController type.
    /// </summary>
    public class ReportController : BaseController
    {
        /// <summary>
        /// Gets the list of reports of the associated customer
        /// GET: /Admin/Report/
        /// </summary>
        /// <returns>Index view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Index()
        {
            return View(Entities.Reports.Where(c => c.ReportCustomerID == Customer.CustomerID));
        }

        /// <summary>
        /// Gets the report associated with the specified id
        /// GET: /Admin/Report/Details/5
        /// </summary>
        /// <param name="id">The report identificator</param>
        /// <returns>Details view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Details(int id)
        {
            var element = Entities.Reports.FirstOrDefault(r => r.ReportCustomerID == Customer.CustomerID && r.ReportID == id);

            if (element == null)
            {
                // TODO: Handle exception
                ViewData.Add("Message", "The specified element does not exist of you do not have privileges to access it.");
                return View("Error");
            }

            return View(element);
        }

        /// <summary>
        /// Gets a category in edit mode so the user can change any field value.
        /// GET: /Admin/Report/Edit/5
        /// </summary>
        /// <param name="id">The report identificator</param>
        /// <returns>Edit mode view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            var entity = Entities.Reports.FirstOrDefault(r => r.ReportID == id);
            var statuses = Entities.Status.Where(s => s.StatusCustomerID == Customer.CustomerID);
            var categories = Entities.Categories.Where(s => s.CategoryCustomerID == Customer.CustomerID);

            var entityViewModel = new ReportViewModel
            {
                Report = entity,
                Statuses = (from status in statuses.AsEnumerable() select new SelectListItem { Text = status.StatusTitle, Value = status.StatusID.ToString() }).ToList(),
                Categories = (from category in categories.AsEnumerable() select new SelectListItem { Text = category.CategoryTitle, Value = category.CategoryID.ToString() }).ToList()
            };

            return View(entityViewModel);
        }

        /// <summary>
        /// Edits the existing report with the given identificator.
        /// POST: /Admin/Report/Edit/5
        /// </summary>
        /// <param name="id">The report identificator</param>
        /// <param name="collection">The form fields collection.</param>
        /// <returns>Edit mode view</returns>
        [HttpPost]
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var entity = Entities.Reports.FirstOrDefault(r => r.ReportID == id);

            try
            {
                UpdateModel(entity, "Report");
                Entities.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(entity);
            }
        }

        /// <summary>
        /// Gets a category in edit mode so the user can change any field value.
        /// GET: /Admin/Report/Fix/5
        /// </summary>
        /// <param name="id">The report identificator</param>
        /// <returns>Edit mode view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Fix(int id)
        {
            var entity = Entities.Reports.FirstOrDefault(r => r.ReportID == id);
            var statuses = Entities.Status.Where(s => s.StatusCustomerID == Customer.CustomerID);
            var categories = Entities.Categories.Where(s => s.CategoryCustomerID == Customer.CustomerID);

            var entityViewModel = new ReportViewModel
            {
                Report = entity,
                Statuses = (from status in statuses.AsEnumerable() select new SelectListItem { Text = status.StatusTitle, Value = status.StatusID.ToString() }).ToList(),
                Categories = (from category in categories.AsEnumerable() select new SelectListItem { Text = category.CategoryTitle, Value = category.CategoryID.ToString() }).ToList()
            };

            return View(entityViewModel);
        }

        /// <summary>
        /// Edits the existing report with the given identificator.
        /// POST: /Admin/Report/Fix/5
        /// </summary>
        /// <param name="id">The report identificator</param>
        /// <param name="collection">The form fields collection.</param>
        /// <returns>Edit mode view</returns>
        [HttpPost]
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Fix(int id, FormCollection collection)
        {
            var entity = Entities.Reports.FirstOrDefault(r => r.ReportID == id);

            try
            {
                UpdateModel(entity, "Report");
                entity.ReportStatusWhen = DateTime.Now;
                entity.ReportFixedImageUrl = "/images/default/nopicture.png";
                Entities.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(entity);
            }
        }

        /// <summary>
        /// Gets a report so the user can select to delete it.
        /// GET: /Admin/Report/Delete/5
        /// </summary>
        /// <param name="id">The report identificator</param>
        /// <returns>Delete confirmation view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            return View(Entities.Reports.FirstOrDefault(r => r.ReportID == id));
        }

        /// <summary>
        /// Deletes the specified id.
        /// POST: /Admin/Report/Delete/5
        /// </summary>
        /// <param name="id">The report identificator</param>
        /// <param name="collection">The form value collection</param>
        /// <returns>Index view</returns>
        [HttpPost]
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var entity = Entities.Reports.FirstOrDefault(r => r.ReportID == id);
                Entities.DeleteObject(entity);
                Entities.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Error");
            }
        }
    }
}
